﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Windows.Forms;
using DosTexPrj.Core.Models;
using DosTexPrj.Core.RepositoriesInterface;
using DosTexPrj.Implement.NHibernate;
using DosTexPrj.UI.WinForm.Commons;
using DosTexPrj.UI.WinForm.Models;
using iTextSharp.text;
using iTextSharp.text.pdf;
using System.IO;

namespace DosTexPrj.UI.WinForm
{
    public partial class DeliveryForm : Form
    {
        #region --- Private methods ---

        private IRepository _repository;
        private IOrderRepository _orderRepository;
        private IList<DeliveryDetail> _currentDeliveriesDetail;
        private Delivery _currentDelivery;

        #endregion --- Private methods ---

        #region --- Properties ---

        public Delivery CurrentDelivery
        {
            private get
            {
                return this._currentDelivery;
            }
            set { this._currentDelivery = value; }
        }

        #endregion --- Properties ---

        #region --- Constructors ---

        public DeliveryForm()
        {
            InitializeComponent();
            _repository = MyServiceLocator.DoGetInstance<IRepository>();
            this._orderRepository = MyServiceLocator.DoGetInstance<IOrderRepository>();
            this._currentDeliveriesDetail = new List<DeliveryDetail>();
        }

        public DeliveryForm(Delivery deliveryToEdit)
            : this()
        {
            this._currentDelivery = deliveryToEdit;
        }

        private bool ClearForm
        {
            set
            {
                txtCustomerAddress.Text = string.Empty;
                txtCustomerName.Text = string.Empty;
                txtCustomerPhone.Text = string.Empty;
                txtDeliveryAddress.Text = string.Empty;

                dtpDeliveryDate.Value = DateTime.Now;
                dtpOrderDate.Value = DateTime.Now;

                lvDeliveryDetail.Items.Clear();

                LoadOrder();
                LoadDeliveryMethod();
            }
        }

        #endregion --- Constructors ---

        private void DeliveryForm_Load(object sender, EventArgs e)
        {
            LoadDeliveryList();
            CreateFormTitle();

            LoadFilterCustomer();
            LoadFilterOrder();

            txtCustomerAddress.Enabled = txtCustomerName.Enabled = txtCustomerPhone.Enabled = btnSave.Enabled = false;
        }

        #region -- Delivery List Grid --

        private void lvDeliveryList_Click(object sender, EventArgs e)
        {
            if (lvDeliveryList.SelectedItems.Count < 1)
            {
                return;
            }
            try
            {
                DeliveryModel selectedDeliveryModel = (DeliveryModel)lvDeliveryList.SelectedItems[0].Tag;
                this.CurrentDelivery = _repository.FindByKey<Delivery>(selectedDeliveryModel.Id);
                this._currentDeliveriesDetail = this.CurrentDelivery.Details;
                PushDataToForm(this.CurrentDelivery);
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.ToString(), "Error", MessageBoxButtons.OK, MessageBoxIcon.Stop);
            }
        }

        private void chkAll_CheckedChanged(object sender, EventArgs e)
        {
            if (chkAll.Checked)
            {
                chkInMonth.Checked = !chkAll.Checked;
                LoadDeliveryList();
            }
            else
            {
                LoadDeliveryList();
            }
        }

        private void chkInMonth_CheckedChanged(object sender, EventArgs e)
        {
            if (chkInMonth.Checked)
            {
                chkAll.Checked = !chkInMonth.Checked;
                LoadDeliveryList();
            }
            else
            {
                LoadDeliveryList();
            }
        }

        private void dtpOrderMonth_ValueChanged(object sender, EventArgs e)
        {
            LoadDeliveryList();
        }

        private void cboCustomerFilter_SelectedIndexChanged(object sender, EventArgs e)
        {
            chkAll.Checked = chkInMonth.Checked = false;
        }

        #endregion -- Delivery List Grid --

        #region -- Delivery detail --

        private void btnAddNew_Click(object sender, EventArgs e)
        {
            ClearForm = true;
            btnSave.Enabled = true;
            this.CurrentDelivery = new Delivery();
            this._currentDeliveriesDetail = new List<DeliveryDetail>();
        }

        private void btnDelete_Click(object sender, EventArgs e)
        {
            if (lvDeliveryList.SelectedItems.Count < 1)
            {
                MessageBox.Show("Please select a Delivery on Delivery List ", "Warning", MessageBoxButtons.OK);
                return;
            }
            try
            {
                DeliveryModel selectedDeliveryModel = (DeliveryModel)lvDeliveryList.SelectedItems[0].Tag;
                if (MessageBox.Show("Are you sure want to delete Delivery No. " + selectedDeliveryModel.Id + "?", "Warning", MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.Yes)
                {
                    _repository.Delete<Delivery>(x => x.Id == selectedDeliveryModel.Id);
                    LoadDeliveryList();
                    ClearForm = true;
                    MessageBox.Show("Delete successfully.", "Warning", MessageBoxButtons.OK);
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.ToString(), "Error", MessageBoxButtons.OK, MessageBoxIcon.Stop);
            }
        }

        private void btnSave_Click(object sender, EventArgs e)
        {
            if (!CheckValidate())
                return;
            this.CreateDelivery();
            btnSave.Enabled = false;
            ClearForm = true;
        }

        private void btnAddOrderDetail_Click(object sender, EventArgs e)
        {
            //Kim edited: do not allow add delivery detail if not choose the order
            if (this._currentDelivery == null)
            {
                MessageBox.Show("Please select order after add delivery detail");
                return;
            }
            //End edited by Kim
            DeliveryDetailForm deliveryDetailForm = new DeliveryDetailForm();

            deliveryDetailForm.CurrentOrder = (Order)cboOrder.SelectedItem;
            deliveryDetailForm.DeliveryDetailsFromDeliveryForm = this._currentDeliveriesDetail.ToList<DeliveryDetail>();

            deliveryDetailForm.FormClosed += new FormClosedEventHandler(orderDetailForm_FormClosed);

            deliveryDetailForm.StartPosition = FormStartPosition.CenterParent;
            deliveryDetailForm.ShowDialog();

            LoadDeliveryDetailList();
        }

        private void orderDetailForm_FormClosed(object sender, FormClosedEventArgs e)
        {
            // Kim edited:
            // Get the DeliveryDetails were processed in the AddDetailForm.
            // No need to use CurrentDeliveryDetail from the AddDetail
            this._currentDeliveriesDetail = (sender as DeliveryDetailForm).DeliveryDetailsFromDeliveryForm;
            // End edited by Kim
        }

        private void btnDeleteOrderDetail_Click(object sender, EventArgs e)
        {
            if (lvDeliveryDetail.SelectedItems.Count < 1)
            {
                MessageBox.Show("Please select a Delivery detail on Delivery Detail List ", "Warning", MessageBoxButtons.OK);
                return;
            }
            try
            {
                DeliveryDetail selectedDeliveryDetail = (DeliveryDetail)lvDeliveryDetail.SelectedItems[0].Tag;

                if (MessageBox.Show("Are you sure want to delete this?", "Warning", MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.Yes)
                {
                    // Kim edited:
                    // just remove from database in case of the Id != Guid.Empty
                    // otherwise, remove from this._currentDeliveriesDetail
                    if (selectedDeliveryDetail.Id != Guid.Empty)
                    {
                        _repository.Delete<DeliveryDetail>(x => x.Id == selectedDeliveryDetail.Id);
                    }
                    this._currentDeliveriesDetail.Remove(selectedDeliveryDetail);
                    //End edited by Kim

                    // Get selected order to refresh detail grid
                    if (lvDeliveryList.SelectedItems.Count >= 1)
                    {
                        DeliveryModel selectedDeliveryModel = (DeliveryModel)lvDeliveryList.SelectedItems[0].Tag;
                    }
                    PushDataToForm(this.CurrentDelivery);
                    MessageBox.Show("Delete successfully.", "Warning", MessageBoxButtons.OK);
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.ToString(), "Error", MessageBoxButtons.OK, MessageBoxIcon.Stop);
            }
        }

        private void cboOrder_SelectedIndexChanged(object sender, EventArgs e)
        {
            Order order = (Order)cboOrder.SelectedItem;
            if (order != null)
            {
                txtCustomerName.Text = order.Customer.CustomerName;
                txtCustomerAddress.Text = order.Customer.Address;
                txtCustomerPhone.Text = order.Customer.PhoneNumber;

                dtpOrderDate.Value = order.OrderDate;
                dtpDeliveryDate.Value = order.DeliveryDate;

                cboDeliveryMethod.SelectedValue = order.DeliveryMethod.Id;
            }
        }

        private void btnPrint_Click(object sender, EventArgs e)
        {
            if (lvDeliveryList.SelectedItems.Count < 1)
            {
                MessageBox.Show("Please select a Delivery on Delivery List for printing", "Warning", MessageBoxButtons.OK);
                return;
            }

            DeliveryModel selectedOrderModel = (DeliveryModel)lvDeliveryList.SelectedItems[0].Tag;
            Delivery order = this._repository.FindByKey<Delivery>(selectedOrderModel.Id);
            if (order != null)
            {
                if (!Directory.Exists(@"C:\pdf"))
                {
                    Directory.CreateDirectory(@"C:\pdf");
                }

                string filePath = @"C:\pdf\printing-delivery-" + order.Id.ToString() + ".pdf";

                DoPrinting(order, filePath);

                if (File.Exists(filePath))
                {
                    System.Diagnostics.ProcessStartInfo printObject = new System.Diagnostics.ProcessStartInfo();
                    printObject.Verb = "print";
                    printObject.FileName = filePath;
                    printObject.UseShellExecute = true;
                    System.Diagnostics.Process.Start(printObject);
                }
            }
            else
            {
                MessageBox.Show("Please select a Delivery on Delivery List for printing", "Warning", MessageBoxButtons.OK);
                return;
            }
        }

        #endregion -- Delivery detail --

        #region -- Functions --

        private bool CheckValidate()
        {
            error.Clear();
            bool flag = true;
            if (!Common.IsBlankTextBox(txtDeliveryAddress, "Required field!", error))
                flag = false;
            if (!Common.IsCorrectDate(dtpOrderDate.Value, dtpDeliveryDate.Value, "Order Date can not be later than Delivery Date!", dtpDeliveryDate, error))
                flag = false;
            if (!Common.IsBlankCombobox(cboOrder, "Required Field", error))
                flag = false;
            if (!Common.IsBlankCombobox(cboDeliveryMethod, "Required Field", error))
                flag = false;
            return flag;
        }

        private void CreateFormTitle()
        {
            ctrTitle.FormTitle = "DELIVERY INFORMATIONS";
            ctrTitle.BackColor = System.Drawing.Color.White;
        }

        private void PushDataToForm(Delivery delivery)
        {
            LoadOrder();
            LoadDeliveryMethod();

            if (delivery != null)
            {
                if (delivery.BelongToOrder != null)
                {
                    dtpOrderDate.Value = delivery.BelongToOrder.OrderDate;

                    cboDeliveryMethod.SelectedItem = delivery.BelongToOrder.DeliveryMethod;

                    if (delivery.BelongToOrder.Customer != null)
                    {
                        txtCustomerAddress.Text = delivery.BelongToOrder.Customer.Address;
                        txtCustomerName.Text = delivery.BelongToOrder.Customer.CustomerName;
                        txtCustomerPhone.Text = delivery.BelongToOrder.Customer.PhoneNumber;
                    }
                }

                txtDeliveryAddress.Text = delivery.DeliveryAddress;

                dtpDeliveryDate.Value = delivery.DeliveryDate.Equals(DateTime.MinValue) ? DateTime.Today : delivery.DeliveryDate;
            }

            lvDeliveryDetail.Items.Clear();

            LoadDeliveryDetailList();

            btnSave.Enabled = true;
        }

        private void LoadDeliveryList()
        {
            var deliveries = _repository.GetAll<Delivery>();
            IList<DeliveryModel> deliverySource = new List<DeliveryModel>();

            // Get delivery in selected month
            if (chkInMonth.Checked)
            {
                // Get delivery by customer
                if (cboOrderFilter.SelectedIndex > 0 && cboCustomerFilter.SelectedIndex > 0)
                {
                    deliverySource = (from delivery in deliveries
                                      where delivery.BelongToOrder.OrderDate.Month == dtpOrderMonth.Value.Month && delivery.BelongToOrder.Id == ((Order)cboOrderFilter.SelectedItem).Id && delivery.BelongToOrder.Customer.Id == ((Customer)cboCustomerFilter.SelectedItem).Id
                                      select new DeliveryModel
                                      {
                                          Id = delivery.Id,
                                          OrderId = delivery.BelongToOrder.Id,
                                          CustomerName = delivery.BelongToOrder.Customer.CustomerName,
                                          OrderDate = delivery.BelongToOrder.OrderDate,
                                          DeliveryDate = delivery.DeliveryDate
                                      }).ToList();
                }
                else if (cboCustomerFilter.SelectedIndex > 0)
                {
                    deliverySource = (from delivery in deliveries
                                      where delivery.BelongToOrder.OrderDate.Month == dtpOrderMonth.Value.Month && delivery.BelongToOrder.Customer.Id == ((Customer)cboCustomerFilter.SelectedItem).Id
                                      select new DeliveryModel
                                   {
                                       Id = delivery.Id,
                                       OrderId = delivery.BelongToOrder.Id,
                                       CustomerName = delivery.BelongToOrder.Customer.CustomerName,
                                       OrderDate = delivery.BelongToOrder.OrderDate,
                                       DeliveryDate = delivery.DeliveryDate
                                   }).ToList();
                }
                // Get delivery by order
                else if (cboOrderFilter.SelectedIndex > 0)
                {
                    deliverySource = (from delivery in deliveries
                                      where delivery.BelongToOrder.OrderDate.Month == dtpOrderMonth.Value.Month && delivery.BelongToOrder.Id == ((Order)cboOrderFilter.SelectedItem).Id
                                      select new DeliveryModel
                                   {
                                       Id = delivery.Id,
                                       OrderId = delivery.BelongToOrder.Id,
                                       CustomerName = delivery.BelongToOrder.Customer.CustomerName,
                                       OrderDate = delivery.BelongToOrder.OrderDate,
                                       DeliveryDate = delivery.DeliveryDate
                                   }).ToList();
                }
                else
                {
                    deliverySource = (from delivery in deliveries
                                      where delivery.BelongToOrder.OrderDate.Month == dtpOrderMonth.Value.Month
                                      select new DeliveryModel
                                   {
                                       Id = delivery.Id,
                                       OrderId = delivery.BelongToOrder.Id,
                                       CustomerName = delivery.BelongToOrder.Customer.CustomerName,
                                       OrderDate = delivery.BelongToOrder.OrderDate,
                                       DeliveryDate = delivery.DeliveryDate
                                   }).ToList();
                }
            }
            // Get all
            else
            {
                // Get delivery by customer
                if (cboOrderFilter.SelectedIndex > 0 && cboCustomerFilter.SelectedIndex > 0)
                {
                    deliverySource = (from delivery in deliveries
                                      where delivery.BelongToOrder.Id == ((Order)cboOrderFilter.SelectedItem).Id && delivery.BelongToOrder.Customer.Id == ((Customer)cboCustomerFilter.SelectedItem).Id
                                      select new DeliveryModel
                                   {
                                       Id = delivery.Id,
                                       OrderId = delivery.BelongToOrder.Id,
                                       CustomerName = delivery.BelongToOrder.Customer.CustomerName,
                                       OrderDate = delivery.BelongToOrder.OrderDate,
                                       DeliveryDate = delivery.DeliveryDate
                                   }).ToList();
                }
                else if (cboCustomerFilter.SelectedIndex > 0)
                {
                    deliverySource = (from delivery in deliveries
                                      where delivery.BelongToOrder.Customer.Id == ((Customer)cboCustomerFilter.SelectedItem).Id
                                      select new DeliveryModel
                                   {
                                       Id = delivery.Id,
                                       OrderId = delivery.BelongToOrder.Id,
                                       CustomerName = delivery.BelongToOrder.Customer.CustomerName,
                                       OrderDate = delivery.BelongToOrder.OrderDate,
                                       DeliveryDate = delivery.DeliveryDate
                                   }).ToList();
                }
                // Get delivery by order
                else if (cboOrderFilter.SelectedIndex > 0)
                {
                    deliverySource = (from delivery in deliveries
                                      where delivery.BelongToOrder.Id == ((Order)cboOrderFilter.SelectedItem).Id
                                      select new DeliveryModel
                                   {
                                       Id = delivery.Id,
                                       OrderId = delivery.BelongToOrder.Id,
                                       CustomerName = delivery.BelongToOrder.Customer.CustomerName,
                                       OrderDate = delivery.BelongToOrder.OrderDate,
                                       DeliveryDate = delivery.DeliveryDate
                                   }).ToList();
                }
                else
                {
                    deliverySource = (from delivery in deliveries
                                      select new DeliveryModel
                                   {
                                       Id = delivery.Id,
                                       OrderId = delivery.BelongToOrder.Id,
                                       CustomerName = delivery.BelongToOrder.Customer.CustomerName,
                                       OrderDate = delivery.BelongToOrder.OrderDate,
                                       DeliveryDate = delivery.DeliveryDate
                                   }).ToList();
                }
            }

            if (deliverySource != null)
            {
                lvDeliveryList.Items.Clear();

                for (int i = 0; i < deliverySource.Count; i++)
                {
                    ListViewItem item = lvDeliveryList.Items.Add(deliverySource[i].Id.ToString());
                    item.Tag = deliverySource[i];
                    item.SubItems.Add(deliverySource[i].OrderId.ToString());
                    item.SubItems.Add(deliverySource[i].CustomerName);
                    item.SubItems.Add(deliverySource[i].OrderDate.ToShortDateString());
                    item.SubItems.Add(deliverySource[i].DeliveryDate.ToShortDateString());
                }
            }
        }

        private void LoadDeliveryDetailList()
        {
            if (this._currentDeliveriesDetail != null)
            {
                lvDeliveryDetail.Items.Clear();

                for (int i = 0; i < this._currentDeliveriesDetail.Count; i++)
                {
                    ListViewItem item = lvDeliveryDetail.Items.Add(i + 1 + "");
                    item.Tag = this._currentDeliveriesDetail[i];
                    item.SubItems.Add(this._currentDeliveriesDetail[i].ReferenceToOrderDetail.FabricType.FabricTypeName);
                    item.SubItems.Add(this._currentDeliveriesDetail[i].ReferenceToOrderDetail.Color.ToString());
                    item.SubItems.Add(this._currentDeliveriesDetail[i].ReferenceToOrderDetail.Composition.CompostionName);
                    item.SubItems.Add(this._currentDeliveriesDetail[i].MtQuantity.ToString());
                    item.SubItems.Add(this._currentDeliveriesDetail[i].KgQuantity.ToString());
                    item.SubItems.Add(this._currentDeliveriesDetail[i].ReferenceToOrderDetail.CuttableWidth.ToString());
                    item.SubItems.Add(this._currentDeliveriesDetail[i].ReferenceToOrderDetail.Weight.ToString());
                }
            }
        }

        private void CreateDelivery()
        {
            var delivery = this.CurrentDelivery;
            delivery.BelongToOrder = (Order)cboOrder.SelectedItem;
            delivery.DeliveryAddress = txtDeliveryAddress.Text.Trim();
            delivery.DeliveryDate = dtpDeliveryDate.Value;

            delivery.Details = this._currentDeliveriesDetail;
            try
            {
                delivery = _repository.Save<Delivery>(delivery);
                this.UpdateOrderDetailWhenDeliveriedDone(delivery);
                LoadDeliveryList();
                MessageBox.Show("Save successfully.", "Warning", MessageBoxButtons.OK);
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.ToString(), "Error", MessageBoxButtons.OK, MessageBoxIcon.Stop);
            }
        }

        // Kim edited: update order detail is done status when delivered it completely
        // Not finish yet
        private void UpdateOrderDetailWhenDeliveriedDone(Delivery delivery)
        {
            var deliveryDetails = delivery.Details;
            var orderDetails = delivery.BelongToOrder.OrderDetails;
            var listOfOrderDetail = (from deliveryDetail in deliveryDetails
                                     group deliveryDetail by deliveryDetail.ReferenceToOrderDetail
                                         into g
                                         select new { OrderDetailID = g }).ToList();
        }

        // End edited by Kim

        private void LoadOrder()
        {
            try
            {
                var orderData = this._orderRepository.LoadAllOrder();
                IList<Order> orders = new List<Order>();

                orders = (from c in orderData
                          select new Order
                             {
                                 Id = c.Id,
                                 Customer = c.Customer,
                                 DeliveryDate = c.DeliveryDate,
                                 DeliveryMethod = c.DeliveryMethod,
                                 Freight = c.Freight,
                                 IsDeliveredBefore = c.IsDeliveredBefore,
                                 NumberOfDateExtensionDelivery = c.NumberOfDateExtensionDelivery,
                                 OrderDate = c.OrderDate,
                                 OrderDetails = c.OrderDetails,
                                 OrderStatus = c.OrderStatus,
                                 PaymentMethod = c.PaymentMethod
                             }).ToList();

                if (orders.Count > -1)
                {
                    cboOrder.DataSource = orders;
                    cboOrder.DisplayMember = "Id";
                    cboOrder.ValueMember = "Id";
                }
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }

        private void LoadDeliveryMethod()
        {
            try
            {
                var deliveryMethodData = _repository.GetAll<DeliveryMethod>();
                IList<DeliveryMethod> deliveryMethods = new List<DeliveryMethod>();

                deliveryMethods = (from d in deliveryMethodData
                                   select new DeliveryMethod
                                   {
                                       Id = d.Id,
                                       DeliveryMethodName = d.DeliveryMethodName
                                   }).ToList();

                if (deliveryMethods.Count > -1)
                {
                    cboDeliveryMethod.DataSource = deliveryMethods;
                    cboDeliveryMethod.DisplayMember = "DeliveryMethodName";
                    cboDeliveryMethod.ValueMember = "Id";
                }
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }

        private void LoadFilterCustomer()
        {
            try
            {
                var customerData = _repository.GetAll<Customer>();
                IList<Customer> customers = new List<Customer>();
                IList<Customer> customersResult = new List<Customer>();

                customers = (from c in customerData
                             select new Customer
                             {
                                 Id = c.Id,
                                 CustomerName = c.CustomerName,
                                 Address = c.Address,
                                 PhoneNumber = c.PhoneNumber
                             }).ToList();

                if (customers.Count > 0)
                {
                    customersResult.Add(new Customer { Id = -1, CustomerName = "-Select a customer-", Address = string.Empty, PhoneNumber = string.Empty });

                    for (int i = 0; i < customers.Count; i++)
                    {
                        customersResult.Add(customers[i]);
                    }
                    cboCustomerFilter.DataSource = customersResult;
                    cboCustomerFilter.DisplayMember = "CustomerName";
                    cboCustomerFilter.ValueMember = "Id";
                }
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }

        private void LoadFilterOrder()
        {
            try
            {
                var orderData = this._orderRepository.LoadAllOrder();
                IList<Order> orderResult = new List<Order>();
                IList<Order> order = new List<Order>();

                order = (from c in orderData
                         select new Order
                         {
                             Id = c.Id,
                             Customer = c.Customer,
                             DeliveryDate = c.DeliveryDate,
                             DeliveryMethod = c.DeliveryMethod,
                             Freight = c.Freight,
                             IsDeliveredBefore = c.IsDeliveredBefore,
                             NumberOfDateExtensionDelivery = c.NumberOfDateExtensionDelivery,
                             OrderDate = c.OrderDate,
                             OrderDetails = c.OrderDetails,
                             OrderStatus = c.OrderStatus,
                             PaymentMethod = c.PaymentMethod
                         }).ToList();

                if (order.Count > 0)
                {
                    orderResult.Add(new Order { Id = -1, Customer = null, DeliveryDate = DateTime.Now, DeliveryMethod = null, Freight = string.Empty, IsDeliveredBefore = false, NumberOfDateExtensionDelivery = 0, OrderDate = DateTime.Now, OrderDetails = null, OrderStatus = null, PaymentMethod = null });
                    for (int i = 0; i < order.Count; i++)
                    {
                        orderResult.Add(order[i]);
                    }
                    cboOrderFilter.DataSource = orderResult;
                    cboOrderFilter.DisplayMember = "Id";
                    cboOrderFilter.ValueMember = "Id";
                }
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }

        private void DoPrinting(Delivery order, string filePath)
        {
            Font fBold = FontFactory.GetFont("Tahoma", 11, iTextSharp.text.Font.BOLD);
            Font fNormal = FontFactory.GetFont("Tahoma", 11);
            Font fNormalySmall = FontFactory.GetFont("Tahoma", 9);
            Font fBig = FontFactory.GetFont("Tahoma", 16, iTextSharp.text.Font.BOLD);

            var pdfFile = new Document(PageSize.A4);
            PdfWriter.GetInstance(pdfFile, new FileStream(filePath, FileMode.Create));

            pdfFile.Open();
            //Add Title
            Printing.AddTitleToPdf(pdfFile, "DELIVERY", fBig);
            // Add Order Info
            DeliveryPrinting.AddOrderInfoToPdf(pdfFile, order, fNormal, fBold);
            // Add Customer Info
            DeliveryPrinting.AddCustomerInfoToPdf(pdfFile, order.BelongToOrder.Customer, fNormal, fBold);
            // Add Order detal
            DeliveryPrinting.AddOrderDetailToPdf(pdfFile, order.Details, fNormalySmall, fBold);
            // Add Signatures
            Printing.AddSignaturesToPdf(pdfFile, fBold, "Notes", "Customer Confirmation");
            pdfFile.Close();
        }

        #endregion -- Functions --

    }
}